<?php
/**
 * Created by PhpStorm.
 * User: refar
 * Date: 22-8-9
 * Time: 上午11:13
 */

namespace app\tasks\controller;

use think\Exception;
use think\Log;

class LogCompress
{
    public function dog() {
        try {
            $today = date("d");

            // 如果是月初
            if ('09' == $today) {

                if (!file_exists(LOG_PATH. 'tar')) {
                    mkdir(LOG_PATH. 'tar', 0755, true);
                }
                // 获取上个月年月
                $lastMonth = date('Ym', strtotime('last month'));
                // 压缩
                if (file_exists(LOG_PATH.$lastMonth)) {
                    Log::record('[LogCompress][dog] start [' . $lastMonth. ']');
                    $tarCmd = 'tar -cjf '. 'tar/'. $lastMonth .'.tar.bz2 ' . $lastMonth . ' && rm -r '. $lastMonth;
                    $cmd = 'cd '. LOG_PATH. ' && ' . $tarCmd;
                    Log::record('[LogCompress][dog] exec ' .$cmd);
                    exec($cmd);
                }

            }

        } catch (Exception $e) {
            Log::record('[LogCompress][dog] error ' . $e->getMessage());
        }

    }
}